package com.tiny;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

public class MaxMin {
    public static void main(String[] args) throws NumberFormatException, IOException {

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(in.readLine());
        int K = Integer.parseInt(in.readLine());
        int[] list = new int[N];

        for(int i = 0; i < N; i ++)
            list[i] = Integer.parseInt(in.readLine());

        int unfairness = Integer.MAX_VALUE;

        unfairness = testGenerate(list, N, K);

        System.out.println(unfairness);
    }

    @Test
    public void test() {
        int [] list = new int[] {
                10,
                20,
                30,
                100,
                101,
                102};
        assertThat(testGenerate(list, 6, 3), is(2));
    }
    public static int testGenerate(int []list, int N, int K) {
        Arrays.sort(list);
        int start = 0, end = 0;
        int length = N - 1;
        int result = Integer.MAX_VALUE;

        for (int index = 0 ; index <= length; index++) {
            start = index;
            end = index + K - 1;
            if (end <= length) {
                result = result < list[end] - list[start] ? result: list[end]-list[start];
            }
        }
        return result;
    }
}
